<?php
class Main extends Controller {

    function __construct()
    {
        parent::Controller();
        $this->load->model('Need_model');
    }


    function index ()
    {
        $this->load->library('session');
        $this->load->helper('form');
        $this->load->helper('date');
        $this->load->helper('string');
        $this->load->helper('url');

        //Check if there is a post and set the hash
        if (isset($_POST['send']) || isset($_POST['resolve'])) {

            //validate
                if($this->session->userdata('form_hash') !== md5($_POST['status'].$_POST['need'].$_POST['obs']))
                {
                    //Guardar
                    if(isset($_POST['send']) && $_POST['send'] == "Guardar")
                    {
                        $this->Need_model->update($_POST['id'], null, $_POST['need'], $_POST['obs']);
                    }
                    //Resuelto
                    if(isset($_POST['resolve']) && $_POST['resolve'] == "Resuelto")
                    {
                        $this->Need_model->update($_POST['id'], "Resuelto", $_POST['need'], $_POST['obs']);
                    }
                    $this->session->set_userdata('form_hash', md5($_POST['status'].$_POST['need'].$_POST['obs']));
                }
        }

        $data['data'] = $this->Need_model->get_where();
        $data['last_id'] = $this->Need_model->last_id();
        $data['count'] = $this->Need_model->count_state();
        $data['nowtime'] = $this->Need_model->get_time();
        $this->load->view('open_html', $data);
        $this->load->view('table_view', $data);
        $this->load->view('close_html', $data);
    }

    

}
?>
